﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CLab.Base;
using System.Collections;

namespace CLab.PagAdminLab
{
    public partial class CatCampoEstudio : PageBase
    {
        #region Eventos

        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 7;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    InicializaPantalla();
                    //CargaGrid();
                }
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }

        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                CargaGrid();
                if (gridCampos.Rows.Count == 0)
                {
                    Master.alertSinDatos();
                }
                SetFocus(txtNombre);
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void gridCampos_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridCampos.PageIndex = e.NewPageIndex;
            CargaGrid();
        }

        protected void btnExportar_Click(object sender, EventArgs e)
        {
            try
            {
                gridCampos.AllowPaging = false;
                CargaGrid();
                gridCampos.ToExportCsv();
                gridCampos.AllowPaging = true;
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void btnElimina_Click(object sender, EventArgs e)
        {
            //
        }

        protected void btnAgregarr_Click(object sender, EventArgs e)
        {
            Response.Redirect("CatCampoEstudioRegistro.aspx?IdEstudio=" + getParameter("IdEstudio") + "&IdCampo=" + hdnIdCampo.Value);
        }

        #endregion Eventos

        #region Métodos

        private void InicializaPantalla()
        {
            Master.TituloPagina = "Campos";
        }

        private void CargaGrid()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();

            //Parámetros
            parametros.Add(CreaParametroConsulta("@pnIdLaboratorio", SessionLaboratorio.IdLaboratorio, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdEstudio", getParameter("IdEstudio"), ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdCampo", hdnIdCampo.Value, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@psNombreCampo", txtNombre.Text, ConsultaServiceReference.Parameter.vEnumType.VarChar));

            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);

            //Consulta
            gridCampos.DataSource = servicio.EjecutaConsultaConParametros("spObtenCampos", prms);
            gridCampos.DataBind();
        }

        #endregion Métodos
    }
}